<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="/html/js/axios.min.js"></script>
    <script src="/html/js/ajax.js"></script>
    <script src="/html/js/layui.js"></script>
    <script src="/html/js/util.js"></script>
    <link rel="stylesheet" href="/html/js/css/layui.css" media="all">
    <title>用户注册</title>
</head>
<script>

    async function addNewUser() {

        let param = {
            username: $("username").value,
            sex: $("sex").value,
            money: $("money").value,
            birthday: $("birthday").value
        }
        let result = await ajaxGet("/user/register", param);
        if (result == "ok") {
            location.href = "/html/userShow.html";
        } else {
            let arrays = document.getElementsByClassName("errorInfo");
            for (let i = 0; i < arrays.length; i++) {
                arrays[i].innerHTML = "";
            }
            for (let i = 0; i < result.length; i++) {
                let objError = result[i];
                console.log(objError);
                $(objError.field + "Div").innerHTML = objError.defaultMessage;
            }
        }
    }
</script>


<body style="padding-top: 50px">
<div style="padding:0 30px;display: flex;justify-content: center">

    <div>
        <h3>添加新用户</h3>
        <br>
        <div id="usernameDiv" class="errorInfo"></div>
        <label class="layui-form-label">姓名</label>
        <input type="text" id="username" required lay-verify="required"
               autocomplete="off"
               class="layui-input" style="width: 200px">
        <br>
        <div id="sexDiv" class="errorInfo"></div>
        <label class="layui-form-label">性别</label>
        <input type="text" id="sex" required lay-verify="required"
               autocomplete="off"
               class="layui-input" style="width: 200px">
        <br>
        <div id="moneyDiv" class="errorInfo"></div>
        <label class="layui-form-label">工资</label>
        <input type="number" id="money" required lay-verify="required"
               autocomplete="off"
               class="layui-input" style="width: 200px">
        <br>
        <div id="birthdayDiv" class="errorInfo"></div>
        <label class="layui-form-label">生日</label>
        <input type="date" id="birthday" required lay-verify="required"
               autocomplete="off"
               class="layui-input" style="width: 200px">
        <br>
        <button type="button" class="layui-btn" onclick="addNewUser()">注册用户</button>
        <button type="button" class="layui-btn"><a href="/html/userShow.html">返回主页</a></button>
    </div>
</div>

</body>
</html>